package utils

import (
	"fmt"
	"net/url"
)

func IfPresent(query url.Values, key string, fun func(value string)) {
	if query.Has(key) {
		fun(query.Get(key))
	}
}

func GetOrderDefault[T comparable](query url.Values, key string, defaultValue T, apply func(str string) (T, error)) T {
	has := query.Has(key)
	if has {
		value := query.Get(key)
		result, err := apply(value)
		if err != nil {
			fmt.Printf("get key %v,parse %v failure,err %v \r\n", key, value, err.Error())
			return defaultValue
		}
		return result
	}
	return defaultValue
}
